﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Stote
{
    public partial class BaseForm : Form
    {
        #region 构造函数
        public BaseForm()
        {
            InitializeComponent();
            pbMin.Click += new EventHandler(pbMin_Click);
            pbMin.MouseEnter += new EventHandler(pbMin_MouseEnter);
            pbMin.MouseLeave += new EventHandler(pbMin_MouseLeave);

            pbMax.Click += new EventHandler(pbMax_Click);
            pbMax.MouseEnter += new EventHandler(pbMax_MouseEnter);
            pbMax.MouseLeave += new EventHandler(pbMax_MouseLeave);

            pbClose.Click += new EventHandler(pbClose_Click);
            pbClose.MouseEnter += new EventHandler(pbClose_MouseEnter);
            pbClose.MouseLeave += new EventHandler(pbClose_MouseLeave);

            panelTitle.MouseDown += PanelTitle_MouseDown;
            // 设置控件使用双缓冲
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
        }
        #endregion

        #region 背景线性渐变
        /*protected override void OnPaintBackground(PaintEventArgs e)
        {

        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            var rectangle = new Rectangle(0, 0, Width, Height);

            using (var brush = new LinearGradientBrush(rectangle, ColorTranslator.FromHtml("#667eea"), ColorTranslator.FromHtml("#764ba2"), LinearGradientMode.ForwardDiagonal))
            {
                e.Graphics.FillRectangle(brush, rectangle);
            }
        }*/
        #endregion

        #region 公开的属性
        private string winText;

        public string WinText
        {
            get { return winText; }
            set
            {
                winText = value;
                lblTitle.Text = winText;
                Refresh();
            }
        }

        #endregion

        #region 无边框窗体拖动及调整大小
        const int HTLEFT = 10;
        const int HTRIGHT = 11;
        const int HTTOP = 12;
        const int HTTOPLEFT = 13;
        const int HTTOPRIGHT = 14;
        const int HTBOTTOM = 15;
        const int HTBOTTOMLEFT = 0x10;
        const int HTBOTTOMRIGHT = 17;
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                /*case 0x0084:
                    base.WndProc(ref m);
                    Point vPoint = new Point((int)m.LParam & 0xFFFF,
                        (int)m.LParam >> 16 & 0xFFFF);
                    vPoint = PointToClient(vPoint);
                    if (vPoint.X <= 5)
                        if (vPoint.Y <= 5)
                            m.Result = (IntPtr)HTTOPLEFT;
                        else if (vPoint.Y >= ClientSize.Height - 5)
                            m.Result = (IntPtr)HTBOTTOMLEFT;
                        else m.Result = (IntPtr)HTLEFT;
                    else if (vPoint.X >= ClientSize.Width - 5)
                        if (vPoint.Y <= 5)
                            m.Result = (IntPtr)HTTOPRIGHT;
                        else if (vPoint.Y >= ClientSize.Height - 5)
                            m.Result = (IntPtr)HTBOTTOMRIGHT;
                        else m.Result = (IntPtr)HTRIGHT;
                    else if (vPoint.Y <= 5)
                        m.Result = (IntPtr)HTTOP;
                    else if (vPoint.Y >= ClientSize.Height - 5)
                        m.Result = (IntPtr)HTBOTTOM;
                    break;
                case 0x0201://鼠标左键按下的消息 
                    m.Msg = 0x00A1;//更改消息为非客户区按下鼠标 
                    m.LParam = IntPtr.Zero;//默认值 
                    m.WParam = new IntPtr(2);//鼠标放在标题栏内 
                    base.WndProc(ref m);
                    break;*/
                case 0x0014: // 禁掉清除背景消息
                    return;
                default:
                    base.WndProc(ref m);  // 移动窗体的核心代码
                    break;
            }
        }

        //防止闪屏代码
        /*protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }*/

        private const int WM_NCLBUTTONDOWN = 0x00A1;
        private const int HT_CAPTION = 0x2;
        private void PanelTitle_MouseDown(object sender, MouseEventArgs e)
        {
            panelTitle.Capture = false;
            Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
            WndProc(ref msg);
        }
        #endregion

        #region 最大化、最小化、关闭
        private void pbMin_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private void pbMin_MouseEnter(object sender, EventArgs e)
        {
            pbMin.BackColor = Color.White;
            pbMin.Image = Properties.Resources.min2;
        }

        private void pbMin_MouseLeave(object sender, EventArgs e)
        {
            pbMin.BackColor = Color.Transparent;
            pbMin.Image = Properties.Resources.min;
        }

        private void pbMax_Click(object sender, EventArgs e)
        {
            WindowState = WindowState == FormWindowState.Maximized ? FormWindowState.Normal : FormWindowState.Maximized;
        }
        private void pbMax_MouseEnter(object sender, EventArgs e)
        {
            pbMax.BackColor = Color.White;
            pbMax.Image = Properties.Resources.max2;
        }

        private void pbMax_MouseLeave(object sender, EventArgs e)
        {
            pbMax.BackColor = Color.Transparent;
            pbMax.Image = Properties.Resources.max;
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            Close();
            Application.Exit();
        }

        private void pbClose_MouseEnter(object sender, EventArgs e)
        {
            pbClose.BackColor = Color.White;
            pbClose.Image = Properties.Resources.close2;
        }

        private void pbClose_MouseLeave(object sender, EventArgs e)
        {
            pbClose.BackColor = Color.Transparent;
            pbClose.Image = Properties.Resources.close1;
        }
        #endregion
    }
}
